package com.ruibang.glass.sale.controller;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruibang.glass.common.domain.camunda.BpmProcessInstanceCreate;
import com.ruibang.glass.sale.constant.Constants;
import com.ruibang.glass.sale.constant.SaleErrorCode;
import com.ruibang.glass.sale.domain.req.GSaleContractManReq;
import com.ruibang.glass.sale.entity.GSaleContractManagement;
import com.ruibang.glass.sale.service.GSaleContractManagementService;
import com.ruibang.glass.sale.service.GSaleContractProductService;
import com.teaming.cloud.framework2.common.exception.ServiceException;
import com.teaming.cloud.framework2.common.model.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author mameng
 */
@Api(tags = "合同管理")
@RestController
@RequestMapping("/api/glass/sale/contract/management")
public class GSaleContractManagementController {
    @Autowired
    private GSaleContractManagementService gSaleContractManagementService;

    @Autowired
    private GSaleContractProductService gSaleContractProductService;

    @PostMapping("list")
    @ApiOperation(value = "合同管理列表", response = ResultBody.class)
    public ResultBody list(@RequestBody GSaleContractManReq deviceAllPageRea) {
        return ResultBody.success(gSaleContractManagementService.getPage(deviceAllPageRea));
    }

    @PostMapping("addGSaleContractManagement")
    @ApiOperation(value = "新增合同", response = ResultBody.class)
    public ResultBody addGSaleContractManagement(@RequestBody GSaleContractManagement gSaleContractManagement) {
        gSaleContractManagementService.addGSaleContractManagement(gSaleContractManagement);
        return ResultBody.success();
    }

    @GetMapping("updateAddGSaleContractManagement")
    @ApiOperation(value = "刷新流程", response = ResultBody.class)
    public ResultBody updateAddGSaleContractManagement() {
        gSaleContractManagementService.updateAddGSaleContractManagement();
        return ResultBody.success();
    }

    @PostMapping("/approveProject")
    @ApiOperation(value = "发起立项审批", response = ResultBody.class)
    public ResultBody approveProject(@RequestBody BpmProcessInstanceCreate BpmProcessInstanceCreate) {
        gSaleContractManagementService.approveProject(BpmProcessInstanceCreate);
        return ResultBody.success();
    }


    @PostMapping("updateGSaleContractManagement")
    @ApiOperation(value = "修改合同", response = ResultBody.class)
    public ResultBody updateGSaleContractManagement(@RequestBody GSaleContractManagement gSaleContractManagement) {
        gSaleContractManagementService.updateGSaleContractManagement(gSaleContractManagement);
        return ResultBody.success();
    }


    @PostMapping("updateDelayGSaleContractManagement")
    @ApiOperation(value = "合同延期", response = ResultBody.class)
    public ResultBody updateDelayGSaleContractManagement(@RequestBody GSaleContractManagement gSaleContractManagement) {
        gSaleContractManagementService.updateDelayGSaleContractManagement(gSaleContractManagement);
        return ResultBody.success();
    }

    @PostMapping("removeByIdOrIds")
    @ApiOperation(value = "删除", response = ResultBody.class)
    public ResultBody removeByIdOrIds(@RequestBody GSaleContractManagement gSaleContractManagement) {
        List<GSaleContractManagement> saleContractManagements = gSaleContractManagementService.listByIds(gSaleContractManagement.getIds());
        for (GSaleContractManagement saleContractManagement : saleContractManagements) {
            if (!StringUtils.equals(saleContractManagement.getApproveStatus(), Constants.ZERO_STR)) {
                throw new ServiceException(SaleErrorCode.SALE_CONTRACT_IS_NO_START.getCode(), SaleErrorCode.SALE_CONTRACT_IS_NO_START.getMsg());
            }
        }
        return ResultBody.success(gSaleContractManagementService.removeBatchByIds(gSaleContractManagement.getIds()));
    }

    @GetMapping("getOne/{id}")
    @ApiOperation(value = "查看详情", response = ResultBody.class)
    public ResultBody getGSaleContractManagementById(@PathVariable("id") String id) {
        GSaleContractManagement byId = gSaleContractManagementService.getGSaleContractManagementById(id);
        return ResultBody.success(byId);
    }

    @GetMapping("getById/{id}")
    @ApiOperation(value = "查看详情", response = ResultBody.class)
    public ResultBody getById(@PathVariable("id") String id) {
        GSaleContractManagement byId = gSaleContractManagementService.getGSaleContractManagementById(id);
        return ResultBody.success(byId);
    }

    @GetMapping("removeProductById/{id}")
    @ApiOperation(value = "删除合同管理关联的数据", response = ResultBody.class)
    public ResultBody removeProductById(@PathVariable("id") String id) {
        gSaleContractProductService.removeById(id);
        return ResultBody.success();
    }

    @GetMapping("getContractSelectList")
    @ApiOperation(value = "获取合同下拉框集合", response = ResultBody.class)
    public ResultBody getContractSelectList() {
        return ResultBody.success(gSaleContractManagementService.getGSaleContractManagementList());
    }

    /**
     * 选择合同后只能使用合同里面的产品
     */
    @GetMapping("/getContractsProductList/{id}")
    @ApiOperation(value = "合同里面的产品下拉列表", response = ResultBody.class)
    public ResultBody getContractsProductList(@PathVariable("id") String id) {
        return ResultBody.success(gSaleContractManagementService.getContractsProductList(id));
    }

    /**
     * 选择合同后只能使用合同里面的产品
     */
    @GetMapping("/getContractNumber")
    @ApiOperation(value = "获取合同编号", response = ResultBody.class)
    public ResultBody getContractNumber() {
        return ResultBody.success(gSaleContractManagementService.generateNextSerialNumber());
    }


}
